//
//  WH_AllOrderViewController.m
//  App_6.23
//
//  Created by wanghao on 16/7/19.
//  Copyright © 2016年 wanghao. All rights reserved.
//

#import "WH_AllOrderViewController.h"
#import "WH_AllOrderCell.h"
#import "WH_Home.h"
#import "WH_OrderDetailVC.h"
#import "AppDelegate.h"
#import "WH_NewDetailViewController.h"
#import "WH_HomeDetailController.h"
@interface WH_AllOrderViewController ()<UITableViewDataSource,UITableViewDelegate,WH_AllOrderCellDelegate>

@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) UIView *selectView;
@property (nonatomic, strong) UIButton *beforeButton;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign) NSInteger Datapage;
@property (nonatomic, assign) NSInteger buttonT;


@end

@implementation WH_AllOrderViewController

-(void)viewWillAppear:(BOOL)animated{
    
    
    [self.tabBarController.tabBar setHidden:YES];
}
-(void)viewWillDisappear:(BOOL)animated{
    
    
    [self.tabBarController.tabBar setHidden:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"夺宝记录";
    self.Datapage = 1;
    [self createOrderEmptylist];
    [self createButton];
    [self createMyTable];
    [self postData:0];
    self.dataArr = [NSMutableArray array];
}
- (void)createMyTable{
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, SCREEN_WIDTH, SCREEN_HEIGHT - 50) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.rowHeight = 210;
    self.myTableView.separatorStyle =     UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.myTableView];
    //自定义cell Xib注册
    [self.myTableView registerNib:[UINib nibWithNibName:@"WH_AllOrderCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"AllOrder"];

    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upLoadMJ)];
    self.myTableView.mj_footer = footer;
    
    [self.myTableView.mj_footer setHidden:YES];

}
#pragma mark - 刷新/加载

-(void)upLoadMJ
{
    NSLog(@"上拉加载");
    self.Datapage++;
    [self postData:_buttonT];
}
#pragma mark - 结束刷新

-(void)endFresh{
    
    [self.myTableView.mj_footer endRefreshing];
    [self.myTableView.mj_header endRefreshing];
}

-(void)createButton{
    
    NSArray *btnArr = [NSArray arrayWithObjects:@"全部", @"进行中", @"已揭晓", nil];
    
    for (int i = 0; i < btnArr.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.titleLabel.font = [UIFont systemFontOfSize:17];
        [button setTitle:btnArr[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.tag = 3000 + i;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(@0);
            make.left.mas_equalTo(@(SCREEN_WIDTH / 3 * i));
            make.width.mas_equalTo(@(SCREEN_WIDTH / 3));
            make.height.mas_equalTo(@50);
        }];
    }
    
    UILabel *xiLabel = [UILabel new];
    xiLabel.backgroundColor = [UIColor colorWithRed:0.91 green:0.91 blue:0.91 alpha:1];
    [self.view addSubview:xiLabel];
    [xiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(@49);
        make.left.right.mas_equalTo(@0);
        make.height.mas_equalTo(@1);
        
    }];
    self.selectView = [[UIView alloc] init];
    self.selectView.backgroundColor = [UIColor colorWithRed:0.82 green:0.13 blue:0.3 alpha:1];
    [self.view addSubview:self.selectView];
    
    [self.selectView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(@48);
        make.left.mas_equalTo(@0);
        make.width.mas_equalTo(@(SCREEN_WIDTH / 3));
        make.height.mas_equalTo(@2);
    }];
    
    UIButton * button = (UIButton *)[self.view viewWithTag:3000];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
}

-(void)buttonAction:(UIButton *)button{
    
    
    if (button.tag != 3000) {
        
        UIButton * buttonA = (UIButton *)[self.view viewWithTag:3000];
        [buttonA setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
    }
    
    if (![button isEqual:self.beforeButton]) {
        
        [self.beforeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        CGFloat x = (button.tag - 3000) * (SCREEN_WIDTH / 3);
        
        [self.selectView mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(@(x));
        }];
        
        [UIView animateWithDuration:0.2 animations:^{
            
            [self.view layoutIfNeeded];
            
        }];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        self.beforeButton = button;
        
    }
    self.Datapage = 1;
    [self.dataArr removeAllObjects];
    [self postData:button.tag - 3000];
    _buttonT = button.tag - 3000;
}
-(void)createOrderEmptylist{
    
    UIImageView *imgView = [[UIImageView alloc] init];
    imgView.tag = 50001;
    imgView.image = [UIImage imageNamed:@"cartImage"];
    imgView.userInteractionEnabled = YES;
    [self.view addSubview:imgView];
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.equalTo(self.view);
        make.top.equalTo(@60);
        make.height.equalTo(@(SCREEN_HEIGHT));
        
    }];
    
    //添加tap
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imBtnAction)];
    [imgView addGestureRecognizer:tap];
    
}
-(void)bringOrderSubView{
    
    UIImageView *imageV = (UIImageView *)[self.view viewWithTag:50001];
    
    [self.view bringSubviewToFront:imageV];
}

-(void)postData:(NSInteger)type{
    
    NSString *body = [NSString stringWithFormat:@"action=order_history&user_id=%@&order_type=%ld&page=%ld",NSUSERID,type, _Datapage];
    
    [WH_AppTools postUrl:URLstr body:body result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        
        //NSLog(@"%@",result);
        NSDictionary *dic = result;
        NSArray *order_array = [dic objectForKey:@"order_array"];
        NSInteger order_quantity = [[dic objectForKey:@"order_quantity"] integerValue];
        //NSLog(@"order_quantity==%ld",order_quantity);

        for (NSDictionary *dic in order_array) {
            
            WH_Home *allOrder = [WH_Home modelWithDictionary:dic];
            [self.dataArr addObject:allOrder];

        }
        //没有数据
        if (_dataArr.count == 0) {
            
            [self bringOrderSubView];
        }else{
            
            [self.view bringSubviewToFront:self.myTableView];
        }
        //加载没有数据
        if (order_quantity == 0) {
            
            self.myTableView.mj_footer.state = MJRefreshStateNoMoreData;
        }else{
            
            
            [self endFresh];
        }
        [self.myTableView reloadData];
        [self.myTableView.mj_footer setHidden:NO];

    } failure:^(NSError *error) {
        
        
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *allOrder = @"AllOrder";
    WH_AllOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:allOrder];
    cell.order = self.dataArr[indexPath.row];
    cell.delegate = self;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WH_NewDetailViewController *detailVC = [[WH_NewDetailViewController alloc] init];
    
    
    WH_Home *newList = self.dataArr[indexPath.row];
    if (newList.user_nick.length != 0) {
        
        //已揭晓
        detailVC.detailNew = newList;
        [self.navigationController pushViewController:detailVC animated:YES];

    }else{
        
        WH_HomeDetailController *homeDetail = [[WH_HomeDetailController alloc] init];
        
        homeDetail.productID = newList.product_id;
        //homeDetail.faceImage = homeModel.product_image;
        //homeDetail.faceTitle = homeModel.product_name;
        [self.navigationController pushViewController:homeDetail animated:YES];
    }

    
}

#pragma mark - 协议方法

-(void)pushToorderDetail:(WH_Home *)model{
    
    WH_OrderDetailVC *detailVC = [[WH_OrderDetailVC alloc] init];
    detailVC.orderModel = model;
    [self.navigationController pushViewController:detailVC animated:YES];
    
}

- (void)imBtnAction{
    
    //UIApplication是系统单例,可以直接使用
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    
    appDelegate.tabbarMain.selectedIndex = 0;
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
